﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Web;
using One.Web.UI.WebControls;

namespace WebApp
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        #region Event Handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name))
            {
                signOut.Visible = true;
                signIn.Visible = false;
            }
            else
            {
                signOut.Visible = false;
                signIn.Visible = true;
            }
            InitTabStrip();
        }

        protected void SignOut(object sender, EventArgs e)
        {
            One.Web.FormsAuthenticationHelper.SignOut(FormsAuthentication.DefaultUrl);
        }

        #endregion

        #region Public Properties

        public string SelectedTab
        {
            set { mainTab.SelectedTab = value; }
            get { return mainTab.SelectedTab; }
        }

        #endregion

        #region Methods

        protected void InitTabStrip()
        {
            try
            {
                SetNavigateUrl4Tab(mainTab, "repairingAtHome", string.Format("{0}?RepairingLocationID={1}", ResolveUrl("~/ReceivingTicket/ReceivingTicketEdit.aspx"), GetParameterValue(Utilities.MyConfig.RepairingLocationAtHomeIDCode, string.Empty)));
                SetNavigateUrl4Tab(mainTab, "repairingAtStation", string.Format("{0}?RepairingLocationID={1}", ResolveUrl("~/ReceivingTicket/ReceivingTicketEdit.aspx"), GetParameterValue(Utilities.MyConfig.RepairingLocationAtStationIDCode, string.Empty)));
            }
            catch (Exception ex)
            {
                Utilities.Utility.ShowMessageBox(Page, ex.ToString());
            }
        }

        protected void SetNavigateUrl4Tab(TabStrip tabStrip, string tabName, string navigateUrl)
        {
            if (tabStrip != null)
            {
                Tab tab = new Tab();
                tab = tabStrip.GetTab(tabName);

                if (tab != null)
                {
                    tab.NavigateUrl = navigateUrl;
                }
            }
        }

        private string GetParameterValue(string code, string defaultValue)
        {
            string value = defaultValue;
            try
            {
                Domain.DAL.ParameterDAO pDAO = Domain.DAL.ParameterDAO.GetInstance();
                if (pDAO != null)
                {
                    Domain.Models.Parameter p = new Domain.Models.Parameter();

                    p = pDAO.GetByCode(code);
                    if (p != null && !string.IsNullOrEmpty(p.Value))
                    {
                        value = p.Value;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return value;
        }

        #endregion
    }
}
